package com.billManage.web.controller.system;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.billManage.common.annotation.Log;
import com.billManage.common.enums.BusinessType;
import com.billManage.system.domain.SysBill;
import com.billManage.system.service.ISysBillService;
import com.billManage.common.core.controller.BaseController;
import com.billManage.common.core.domain.AjaxResult;
import com.billManage.common.utils.poi.ExcelUtil;
import com.billManage.common.core.page.TableDataInfo;

/**
 * 票据管理Controller
 * 
 * @author ruoyi
 * @date 2024-05-14
 */
@Controller
@RequestMapping("/system/bill")
public class SysBillController extends BaseController
{
    private String prefix = "system/bill";

    @Autowired
    private ISysBillService sysBillService;

    @RequiresPermissions("system:bill:view")
    @GetMapping()
    public String bill()
    {
        return prefix + "/bill";
    }

    @RequiresPermissions("system:bill:viewCw")
    @GetMapping("/Cw")
    public String billCw()
    {
        return prefix + "/billCw";
    }

    /**
     * 查询票据管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBill sysBill)
    {
        startPage();
        sysBill.setCreateBy(this.getLoginName());
        List<SysBill> list = sysBillService.selectSysBillList(sysBill);
        return getDataTable(list);
    }

    /**
     * 查询票据管理列表
     */
    @PostMapping("/listCw")
    @ResponseBody
    public TableDataInfo listCw(SysBill sysBill)
    {
        startPage();
        List<SysBill> list = sysBillService.selectSysBillList(sysBill);
        return getDataTable(list);
    }

    /**
     * 导出票据管理列表
     */
    @Log(title = "票据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysBill sysBill)
    {
        List<SysBill> list = sysBillService.selectSysBillList(sysBill);
        ExcelUtil<SysBill> util = new ExcelUtil<SysBill>(SysBill.class);
        return util.exportExcel(list, "票据管理数据");
    }

    /**
     * 新增票据管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存票据管理
     */
    @Log(title = "票据管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysBill sysBill)
    {
        SysBill bill = sysBillService.selectSysBillOne(sysBill);
        if(null != bill){
            return  AjaxResult.error("发票编号已存在");
        }
        sysBill.setCreateBy(this.getLoginName());
        return toAjax(sysBillService.insertSysBill(sysBill));
    }

    /**
     * 修改票据管理
     */
    @GetMapping("/edit/{billId}")
    public String edit(@PathVariable("billId") Long billId, ModelMap mmap)
    {

        SysBill sysBill = sysBillService.selectSysBillByBillId(billId);
        mmap.put("sysBill", sysBill);
        return prefix + "/edit";
    }


    /**
     * 修改票据管理
     */
    @GetMapping("/editCw/{billId}")
    public String editCw(@PathVariable("billId") Long billId, ModelMap mmap)
    {

        SysBill sysBill = sysBillService.selectSysBillByBillId(billId);
        mmap.put("sysBill", sysBill);
        return prefix + "/editCw";
    }


    /**
     * 修改保存票据管理
     */
    @Log(title = "票据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysBill sysBill)
    {
        if(sysBill.getStatus().equals("2") && StringUtils.isEmpty(sysBill.getReason())){
            return   AjaxResult.error("请填写审核未通过原因！");
        }
        sysBill.setUpdateBy(this.getLoginName());
        sysBill.setReviewBy(this.getLoginName());
        sysBill.setReviewTime(new Date());
        return toAjax(sysBillService.updateSysBill(sysBill));
    }

    /**
     * 删除票据管理
     */
    @Log(title = "票据管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysBillService.deleteSysBillByBillIds(ids));
    }
}
